define(["layuiall","zTree","jquery","jq_request","helper"],function (layui,zTree,$,jq_request,helper){
    var Controller ={
        index:function () {
            layui.use(["form"],function (form) {
                form.on('switch(isMenu)', function(data){
                    var is_menu = 0;
                    var prarent = 0;
                    if(data.elem.checked){
                        is_menu = 1
                        //选择层级菜单
                        var TreeHtml = '<ul id="MenuTree" class="ztree"></ul>';
                        var menuTreeAler = layer.open({
                            type: 1,
                            title: "层级选择",
                            content: TreeHtml,
                            area:["300px","350px"],
                            shadeClose:true,
                            success: function () {
                                jq_request.post($(data.elem).attr("menu-url"), {
                                    data: {
                                        menuType: 'tree'
                                    },
                                    success: function (res) {
                                        var zTreeObj;
                                        var setting = {
                                            view: {
                                                showIcon: true
                                            },
                                            data: {
                                                simpleData: {
                                                    enable: true,
                                                    idKey:"id",
                                                    pIdKey:"parent_id",
                                                },
                                                key:{
                                                    name:"text"
                                                }
                                            },
                                            check: {
                                                enable: true,
                                                chkStyle: "radio",
                                                radioType: "all"
                                            },
                                            callback: {
                                                onClick: function (e, treeId, treeNode, clickFlag) {
                                                    zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                                                    nodes = zTreeObj.getCheckedNodes(true),
                                                        v = "";
                                                    for (var i = 0; i < nodes.length; i++) {
                                                        v += nodes[i].id
                                                        prarent = v
                                                    }
                                                    ajxAddmenu({
                                                        id:data.value,
                                                        is_menu:is_menu,
                                                        parent_id:prarent
                                                    },$(data.elem).attr("data-url"));
                                                    layer.close(menuTreeAler);
                                                },
                                                onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                                                    prarent = treeNode.id
                                                    ajxAddmenu({
                                                        id:data.value,
                                                        is_menu:is_menu,
                                                        parent_id:prarent
                                                    },$(data.elem).attr("data-url"));
                                                    layer.close(menuTreeAler)
                                                },
                                            }
                                        };
                                        zTreeObj = $.fn.zTree.init($("#MenuTree"), setting, res);
                                        zTreeObj.expandAll(true);
                                    }
                                })
                            }
                        })
                    }
                    if(is_menu===0){
                        ajxAddmenu({
                            id:data.value,
                            is_menu:is_menu
                        },$(data.elem).attr("data-url"));
                    }
                });
                form.on('switch(auth)', function(data){
                    var auth = 0;
                    if(data.elem.checked){
                        auth = 1
                    }
                    console.log(auth)
                    ajxAddmenu({
                        id:data.value,
                        auth:auth
                    },$(data.elem).attr("data-url"));
                });
            });

            $(".ref-node").click(function () {
                layer.open({
                    skin:'refNode_iframe',
                    title:'菜单刷新',
                    type:2,
                    content:$(this).attr("data-url"),
                    area:["650px","450px"],
                    offset: '100px',
                    shadeClose:true
                })
            })
            $(".clearExceptionNode").click(function () {
                jq_request.get($(this).attr("data-url"),[])
            })
        },
        refnode:function () {
            $(".back").click(function () {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            })
        }
    };

    //提交菜单
    function ajxAddmenu(data,url) {
        $.ajax({
            type:'post',
            url:url,
            data:data,
            success:function (res) {
                layer.msg(res.msg)
            }
        })
    }

    return Controller;
});